import {
	EditFormActionType,
	ICtrlDataAction,
	ICtrlEvent,
	IEditFormAbility,
	IEditFormControllerParams,
	IEditFormModel,
	IEditFormStore,
	IParam,
} from '@/core';
import { QXEvent } from 'qx-util';
import { PropType } from 'vue';
import {
	getCtrlClassNames,
	useCtrlAction,
	useCtrlBasic,
	useCtrlControllerParams,
	useCtrlProps,
} from '../use-ctrl';

/**
 * 表单输入参数
 *
 * @export
 * @return {*}
 */
export function useFormProps() {
	return {
		...useCtrlProps(),
		openView: Function,
		newView: Function,
		actions: Object as PropType<ICtrlDataAction>,
		isEditable: {
			type: Boolean,
			default: false,
		},
		autoLoad: {
			type: Boolean,
			default: false,
		},
		localMode: {
			type: Boolean,
			default: false,
		},
	};
}

/**
 * 表单基础参数
 *
 * @export
 * @param {IEditFormModel} model
 * @param {IParam} props
 * @return {*}
 */
export function useFormBasic(model: IEditFormModel, props: IParam) {
	const getClassNames = (props: IParam) => {
		return getCtrlClassNames(model, props);
	};
	return {
		...useCtrlBasic(model, props),
		getClassNames,
	};
}

/**
 * 表单部件行为
 *
 * @export
 */
export function useFormAction(controller: any) {
	return {
		...useCtrlAction(controller),
	};
}

/**
 * 表单部件控制器
 *
 * @export
 */
export function useFormControllerParams<
	S extends IEditFormStore,
	M extends IEditFormModel
>(
	model: M,
	props: any,
	evt: QXEvent<ICtrlEvent<EditFormActionType, IEditFormAbility>>,
	otherParams: IParam = {}
): IEditFormControllerParams<EditFormActionType, IEditFormAbility> {
	return {
		...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
		formValidate: async () => {
			return await otherParams.editForm.value.formValidate();
		},
		formValidateFields: async (name: string[]) => {
			return await otherParams.editForm.value.formValidateFields(name);
		},
		actions: props.actions,
		openView: props.openView,
		newView: props.newView,
		isEditable: props.isEditable,
		autoLoad: props.autoLoad,
	};
}
